package com.app.utils;

import com.core.lib.utils.main.UIHelper;
import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SynthesizerListener;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class PlaySoundXfUtil {
    private Context context;
    private SpeechSynthesizer mTts;
    private boolean isFinish = true;   //播完状态
    private boolean isPause = false;   //不在暂停状态

    private ImageView mButtonYuYinImageView;
    private TextView mButtonYuYinTextView;

    public PlaySoundXfUtil(Context context) {
        this.context = context;
        // 初始化合成对象
        mTts = SpeechSynthesizer.createSynthesizer(context, mTtsInitListener);
    }

    // 初始化监听
    private InitListener mTtsInitListener = new InitListener() {
        @Override
        public void onInit(int code) {
            if (code != ErrorCode.SUCCESS) {
                //MyToast.show(context, "初始化失败,错误码：" + code, Toast.LENGTH_SHORT);
            }
        }
    };

    private String dowithText(String text)
    {
        try {
            if(text.length() > 1900)
            {
                text = text.substring(0,1900);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return text;
    }

    //播放文本
    public void playSound(String text, ImageView buttonYuYinImageView) {

        mButtonYuYinImageView = buttonYuYinImageView;
        if (isFinish && !isPause) {
            setParam();
            text = dowithText(text);
            int code = mTts.startSpeaking(text, mTtsListener);
            if (code != ErrorCode.SUCCESS) {
                UIHelper.showToast(context, "播放失败,请重试");
                buttonYuYinImageView.setSelected(false);
            } else {
                isFinish = false;
                buttonYuYinImageView.setSelected(true);
            }
        } else if (!isFinish && !isPause) {
            mTts.pauseSpeaking();
            isPause = true;
            buttonYuYinImageView.setSelected(false);

        } else if (!isFinish && isPause) {
            mTts.resumeSpeaking();
            isPause = false;
            buttonYuYinImageView.setSelected(true);
        }
    }

    //播放文本
    public void playSound(String text, TextView buttonYuYinTextView) {

        mButtonYuYinTextView = buttonYuYinTextView;
        if (isFinish && !isPause) {
            setParam();
            text = dowithText(text);
            int code = mTts.startSpeaking(text, mTtsListener);
            if (code != ErrorCode.SUCCESS) {
                UIHelper.showToast(context, "播放失败,请重试");
                buttonYuYinTextView.setSelected(false);
            } else {
                isFinish = false;
                buttonYuYinTextView.setSelected(true);
            }
        } else if (!isFinish && !isPause) {
            mTts.pauseSpeaking();
            isPause = true;
            buttonYuYinTextView.setSelected(false);

        } else if (!isFinish && isPause) {
            mTts.resumeSpeaking();
            isPause = false;
            buttonYuYinTextView.setSelected(true);
        }
    }

    //播放语音
    public void playSound(MediaPlayer mediaPlayer, ImageView buttonYuYin) {

        if (!buttonYuYin.isSelected()) {
            if (!mediaPlayer.isPlaying()) {
                mediaPlayer.start();
                buttonYuYin.setSelected(true);
            } else {
                mediaPlayer.start();
                buttonYuYin.setSelected(true);
            }
        } else {
            if (!mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
                buttonYuYin.setSelected(false);
            } else {
                mediaPlayer.pause();
                buttonYuYin.setSelected(false);
            }
        }

    }

    public void stopSound() {
        mTts.stopSpeaking();
        // 退出时释放连接
        mTts.destroy();

        isPause = false;
        isFinish = true;
    }


    /**
     * 合成回调监听。
     */
    private SynthesizerListener mTtsListener = new SynthesizerListener() {
        @Override
        public void onSpeakBegin() {
            //	showTip("开始播放");
        }

        @Override
        public void onSpeakPaused() {
            //	showTip("暂停播放");
        }

        @Override
        public void onSpeakResumed() {
            //	showTip("继续播放");
        }

        @Override
        public void onBufferProgress(int percent, int beginPos, int endPos,
                                     String info) {
            //缓存进度
//			mPercentForBuffering = percent;
//			showTip(String.format(getString(R.string.tts_toast_format),
//					mPercentForBuffering, mPercentForPlaying));
        }

        @Override
        public void onSpeakProgress(int percent, int beginPos, int endPos) {
            //播放进度
//			mPercentForPlaying = percent;
//			showTip(String.format(getString(R.string.tts_toast_format),
//					mPercentForBuffering, mPercentForPlaying));
        }

        @Override
        public void onCompleted(SpeechError error) {
            if (error == null) {
                //	showTip("播放完成");
                isFinish = true;
            } else if (error != null) {
                //showTip(error.getPlainDescription(true));
                isFinish = true;
                if(mButtonYuYinImageView != null)
                {
                    mButtonYuYinImageView.setSelected(false);
                }

                if(mButtonYuYinTextView != null)
                {
                    mButtonYuYinTextView.setSelected(false);
                }

                UIHelper.showToast(context, "播放失败,请重试。"+error.getPlainDescription(true));
            }
        }

        @Override
        public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {

        }
    };

    private void showTip(String text) {

    }


    //参数设置
    private void setParam() {
        // 清空参数
        mTts.setParameter(SpeechConstant.PARAMS, null);
        // 设置合成
        mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
        // 设置发音人
        mTts.setParameter(SpeechConstant.VOICE_NAME,
                "xiaoyan");

        // 设置语速
        mTts.setParameter(SpeechConstant.SPEED, "50");

        // 设置音调
        mTts.setParameter(SpeechConstant.PITCH, "50");

        // 设置音量
        mTts.setParameter(SpeechConstant.VOLUME, "50");

        // 设置播放器音频流类型
        mTts.setParameter(SpeechConstant.STREAM_TYPE, "3");

    }
}
